<script setup>
const weeks = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
import { invoke } from '@tauri-apps/api/tauri'
const myIframe = ref(null);
import { onMounted, ref,toRefs } from 'vue';
import moment from 'moment'
     const getFirstDayOfWeek = (date) =>{
      let weekday = date.getDay() || 7 //获取星期几,getDay()返回值是 0（周日） 到 6（周六） 之间的一个整数。0||7为7，即weekday的值为1-7
      let start = new Date(date.getTime())
      start.setDate(date.getDate() - weekday + 1) //往前算（weekday-1）天，年份、月份会自动变化
      return start
    };
    const timeFormat = (date)=> {
      if (!date || typeof date === 'string') {
        this.error('参数异常，请检查...')
      }
      let y = date.getFullYear() //年
      let m = date.getMonth() + 1 //月
      let d = date.getDate() //日
      let ds = d + ''
      if (d < 10) {
        ds = '0' + d
      }
      let ms = m + ''
      if (m < 10) {
        ms = '0' + m
      }
      return y + '-' + ms + '-' + ds
    };
      const requestOptions = {
          method: 'GET',
          mode: "no-cors",
          headers: {
            'Access-Control-Allow-Origin': '*',
            "Content-Type": "application/json",
          },
          cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
        };
    const getData = async () => {
     const url =  'https://ptdx.shpt.gov.cn/boot/room/dbRoomUse/listApply1?sdate=' + timeFormat(startDate.value) + '&edate=' + timeFormat(endDate.value)
     

    const response = await invoke('make_http_request', { url });
    let res = JSON.parse(response)
     dataList.value = res.result;
    }
    const weekDay = (d) =>{
      if (d == 0) {
        startDate.value = getFirstDayOfWeek(new Date())
        endDate.value = getLatestDayOfWeek(new Date())
      } else if (d == -1) {
        startDate.value = new Date(startDate.value.getTime() - 7 * 24 * 60 * 60 * 1000)
        endDate.value = new Date(startDate.value.getTime() + 7 * 24 * 60 * 60 * 1000)
      } else if (d == 1) {
        startDate.value = new Date(startDate.value.getTime() + 7 * 24 * 60 * 60 * 1000)
        endDate.value = new Date(startDate.value.getTime() + 7 * 24 * 60 * 60 * 1000)
      } 
      dateList.value = []
      for(let i = 0; i < 7; i ++) {
        let d = new Date();
        d.setTime(startDate.value.getTime() + i*1000*60*60*24);
        dateList.value.push(d)
      }
      getData();
    };
    //获取这周的周一
const getLatestDayOfWeek = (date) =>{
      let weekday = date.getDay() || 7 //获取星期几,getDay()返回值是 0（周日） 到 6（周六） 之间的一个整数。0||7为7，即weekday的值为1-7
      console.log(date)
      let end = new Date(date.getTime())
      end.setDate(date.getDate() - weekday + 7) //往前算（weekday-1）天，年份、月份会自动变化
      return end
    };

    //获取当月第一天
  const   getFirstDayOfMonth = (date) => {
      date.setDate(1)
      return new Date(date.getTime())
    };
   const  getListData = (value) => {
      let listData = []
      if (this.dataList && this.dataList.length > 0) {
        for (let i = 0; i < this.dataList.length; i++) {
          let d = this.dataList[i]
          let date = moment(d.usedate)
          let datadate = date.date()
          if (value.date() == datadate) {
            let content = d.roomname + ' ' + d.updown
            let type = 'warning'
            if (d.flagstate == 1) {
              type = 'success'
            }
            listData.push({ type: type, content: content })
          }
        }
      }
      return listData || []
    };

    //获取当季第一天
  const   getFirstDayOfSeason = (date) => {
      let month = date.getMonth()
      if (month < 3) {
        date.setMonth(0)
      } else if (2 < month && month < 6) {
        date.setMonth(3)
      } else if (5 < month && month < 9) {
        date.setMonth(6)
      } else if (8 < month && month < 11) {
        date.setMonth(9)
      }
      date.setDate(1)
      return date
    };

    //获取当年第一天
   const  getFirstDayOfYear = (date) => {
      date.setDate(1)
      date.setMonth(0)
      return date
    };
    const timeFormat1 = (date)=> {
      if (!date || typeof date === 'string') {
        error('参数异常，请检查...')
      }
      let y = date.getFullYear() //年
      let m = date.getMonth() + 1 //月
      let d = date.getDate() //日
      let ds = d + ''
      if (d < 10) {
        ds = '0' + d
      }
      let ms = m + ''
      if (m < 10) {
        ms = '0' + m
      }
      return ms + '月' + ds + '日'
    };
    const format = (date)=>{
      const day = date.getDate();
      const month = date.getMonth() + 1;
      const year = date.getFullYear();
      let days = '' + day;
      if(day < 10) {
        days = '0' + day;
      }
      let months = '' + month;
      if(month < 10) {
        months = '0'+month;
      }
      return `${year}-${months}-${days}`
    };

    const getWeek = (item)=> {
      let date = new Date(item)
      return weeks[date.getDay()]
    };
   const getRoom = (i, item, updown) =>{
      let itemList = []
      if(!dataList.value) {
        dataList.value = []
      }
      for (let i = 0; i < dataList.value.length; i++) {
        let css = dataList.value[i]
        if (timeFormat(item) == css.usedate && updown === css.updown) {
          itemList.push(css)
        }
      }
      if (itemList.length > i) {
        let it = itemList[i]
        let str = it.classname + '('
        if (it.uselunch != null && it.uselunch != undefined && it.uselunch == 1) {
          str = str + '<strong>' + +it.studnum + '人)' + '</strong>'
        } else {
          str = str + +it.studnum + '人)'
        }
        str = str + '<br/>'
        if (it.sublist.length > 0) {
          for (let j = 0; j < it.sublist.length; j++) {
            let su = it.sublist[j]
            str = str + (j + 1) + '、' + su.starttime + su.specialname
            if (
              su.specialteachername != null &&
              su.specialteachername != undefined &&
              su.specialteachername.length > 1
            ) {
              str = str + ' ( <strong>' + su.specialteachername + '</strong>)'
            }
            str = str + ' (' + su.roomname + ')' + '<br/>'
          }

          return str
        } else {
          str = str + it.starttime + it.specialname
          if (it.specialteachername != null && it.specialteachername != undefined && it.specialteachername.length > 1) {
            str = str + ' ( <strong>' + it.specialteachername + '</strong>)'
          }
          str = str + ' (' + it.roomname + ')' + '<br/>'
          return str
        }
      }
      return ''
    };
    const getWeekDay = (room, week, updown) =>{
      let str = []
      for (let i = 0; i < this.dataList.length; i++) {
        let css = this.dataList[i]
        let d = new Date(css.usedate)
        let weekday = d.getDay() || 7
        if (css.roomid == room.roomid && css.updown == updown && week == weekday) {
          str.push(css.classname)
        }
      }
      return str.join(',')
    };
    const  getContentFromIframe = ()=> {
     const  iframe = document.getElementById("myIframe");
     if(iframe) {
        const iframeContent = iframe.contentDocument || iframe.contentWindow.document;
        if (iframeContent) {
          console.log(iframeContent.documentElement.innerHTML); // 输出整个 HTML 内容
        }
     }
      // 检查是否能够访问 iframe 的内容
      if (iframe && iframe.contentDocument) {
        const iframeContent = iframe.contentDocument;
        console.log(iframeContent.innerHTML)
      }
    }
let  startDate =  ref();
startDate.value = getFirstDayOfWeek(new Date())
let  endDate = ref()
endDate.value = getLatestDayOfWeek(new Date())
let  start = timeFormat1(startDate.value)
let  end = timeFormat1(endDate.value)
let dataList = ref();
const dateList = ref();
onMounted(()=>{
  weekDay(0)
})
</script>
<template>
  <div class="container">
    <table
      border="0"
      align="center"
      cellspacing="1"
      cellpadding="0"
      class="table"
    >
      <tbody>
        <tr>
          <td colspan="3" align="center" style="display: flex">
            开始日期:
            <VueDatePicker
              v-model="startDate" :format="format"
              placeholder="请选择日期"
              style="width: 170px; height: 20px; margin-left: 20px"
            ></VueDatePicker>
            结束日期:
            <VueDatePicker
              v-model="endDate" :format="format"
              placeholder="请选择日期"
              style="width: 170px; height: 20px; margin-left: 20px"
            ></VueDatePicker>
            &nbsp;
            <input
              type="button"
              name="btnlastweek"
              value=" 查询 "
              @click="weekDay(-100)"
            />
            &nbsp;
            <input
              type="button"
              name="btnlastweek"
              value=" 上一周 "
              @click="weekDay(-1)"
            />
            &nbsp;
            <input
              type="button"
              name="btnthisweek"
              value=" 本 周 "
              @click="weekDay(0)"
            />
            &nbsp;
            <input
              type="button"
              name="btnnextweek"
              value=" 下一周 "
              @click="weekDay(1)"
            />
            &nbsp;
          </td>
        </tr>
        <tr style="margin-top: 10px">
          <td height="22" colspan="3" align="center"></td>
        </tr>
      </tbody>
    </table>
    <table
      border="0"
      align="center"
      cellspacing="1"
      cellpadding="0"
      class="table"
    >
      <tbody id="reportdataarea1111" ref="reportdataarea1111">
        <tr style="margin-top: 10px">
          <td height="22" colspan="3" align="center">
            <span
              class="chked"
              style="font-family: 幼圆; font-size: 22pt; font-weight: bold"
              >班级一览表
              <span style="font-size: 9pt">({{ start }}--{{ end }})</span>
            </span>
          </td>
        </tr>
        <tr>
          <td>
            <table
              width="100%"
              border="0"
              cellpadding="0"
              cellspacing="1"
              bgcolor="#68AEF1"
            >
              <tbody>
                <tr>
                  <td
                    colspan="3"
                    align="right"
                    bgcolor="#b6dbff"
                    id="timeschedule"
                  >
                    <table
                      width="100%"
                      cellpadding="1"
                      cellspacing="1"
                      border="1"
                      bgcolor="#b6dbff"
                    >
                      <thead>
                        <tr>
                          <td
                            style="
                              border-bottom: 1px solid #000;
                              border-left: 1px solid #000;
                              border-right: 1px solid #000;
                              width: 70px;
                            "
                          ></td>
                          <td
                            style="
                              border-bottom: 1px solid #000;
                              border-left: 1px solid #000;
                              border-right: 1px solid #000;
                              width: 300px;
                            "
                            align="center"
                          >
                            班级一
                          </td>
                          <td
                            style="
                              border-bottom: 1px solid #000;
                              border-left: 1px solid #000;
                              border-right: 1px solid #000;
                              width: 300px;
                            "
                            align="center"
                          >
                            班级二
                          </td>
                          <td
                            style="
                              border-bottom: 1px solid #000;
                              border-left: 1px solid #000;
                              border-right: 1px solid #000;
                              width: 300px;
                            "
                            align="center"
                          >
                            班级三
                          </td>
                          <td
                            style="
                              border-bottom: 1px solid #000;
                              border-left: 1px solid #000;
                              border-right: 1px solid #000;
                              width: 300px;
                            "
                            align="center"
                          >
                            班级四
                          </td>
                        </tr>
                      </thead>
                      <tbody>
                        <template
                          v-for="(item, index) in dateList"
                          :key="index"
                        >
                          <tr>
                            <td
                              align="center"
                              valign="middle"
                              rowspan="2"
                              style="
                                font-family: 幼圆;
                                font-size: 12pt;
                                font-weight: bold;
                                border-bottom: 1px solid #000;
                                border-left: 1px solid #000;
                                border-right: 1px solid #000;
                                width: 70px;
                              "
                            >
                              {{ timeFormat1(item) }}
                              <br />{{ getWeek(item) }}
                            </td>
                            <td
                              style="
                                border-bottom: 1px solid #000;
                                border-left: 1px solid #000;
                                border-right: 1px solid #000;
                                font-size: 9pt;
                                padding: 2px;
                              "
                              v-html="getRoom(0, item, '上午')"
                            ></td>
                            <td
                              style="
                                border-bottom: 1px solid #000;
                                border-left: 1px solid #000;
                                border-right: 1px solid #000;
                                font-size: 9pt;
                                padding: 2px;
                              "
                              v-html="getRoom(1, item, '上午')"
                            ></td>
                            <td
                              style="
                                border-bottom: 1px solid #000;
                                border-left: 1px solid #000;
                                border-right: 1px solid #000;
                                font-size: 9pt;
                                padding: 2px;
                              "
                              v-html="getRoom(2, item, '上午')"
                            ></td>
                            <td
                              style="
                                border-bottom: 1px solid #000;
                                border-left: 1px solid #000;
                                border-right: 1px solid #000;
                                font-size: 9pt;
                                padding: 2px;
                              "
                              v-html="getRoom(3, item, '上午')"
                            ></td>
                          </tr>
                          <tr>
                            <td
                              style="
                                border-bottom: 1px solid #000;
                                border-left: 1px solid #000;
                                border-right: 1px solid #000;
                                font-size: 9pt;
                                padding: 2px;
                              "
                              v-html="getRoom(0, item, '下午')"
                            ></td>
                            <td
                              style="
                                border-bottom: 1px solid #000;
                                border-left: 1px solid #000;
                                border-right: 1px solid #000;
                                font-size: 9pt;
                                padding: 2px;
                              "
                              v-html="getRoom(1, item, '下午')"
                            ></td>
                            <td
                              style="
                                border-bottom: 1px solid #000;
                                border-left: 1px solid #000;
                                border-right: 1px solid #000;
                                font-size: 9pt;
                                padding: 2px;
                              "
                              v-html="getRoom(2, item, '下午')"
                            ></td>
                            <td
                              style="
                                border-bottom: 1px solid #000;
                                border-left: 1px solid #000;
                                border-right: 1px solid #000;
                                font-size: 9pt;
                                padding: 2px;
                              "
                              v-html="getRoom(3, item, '下午')"
                            ></td>
                          </tr>
                        </template>
                      </tbody>
                    </table>
                  </td>
                </tr>
              </tbody>
            </table>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<style scoped>
.logo.vite:hover {
  filter: drop-shadow(0 0 2em #747bff);
}

.logo.vue:hover {
  filter: drop-shadow(0 0 2em #249b73);
}
</style>
